@extends('layouts.application')
@section('content')

    <!-- content start -->
    <div class="admin-content">
        <div class="admin-content-body">
            <div class="am-cf am-padding am-padding-bottom-0">
                <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">商品分类</strong> /
                    <small>Shop Category</small>
                </div>
            </div>
            @include('layouts.shared._flash')
            <hr>

            <div class="am-g">
                <div class="am-u-sm-12 am-u-md-6">
                    <div class="am-btn-toolbar">
                        <div class="am-btn-group am-btn-group-xs">
                            <a href="{{route('shop.category.create')}}" type="button"
                               class="am-btn am-btn-default"><span class="am-icon-plus"></span> 新增
                            </a>
                            <button type="button" class="am-btn am-btn-success" id="show_all"><span
                                        class="am-icon-arrows-alt"></span>
                                展开所有
                            </button>
                        </div>
                    </div>
                </div>
                <div class="am-u-sm-12 am-u-md-3">
                    <div class="am-form-group">
                        <select data-am-selected="{btnSize: 'sm'}">
                            <option value="option1">所有类别</option>
                            <option value="option2">IT业界</option>

                        </select>
                    </div>
                </div>
                <div class="am-u-sm-12 am-u-md-3">
                    <div class="am-input-group am-input-group-sm">
                        <input type="text" class="am-form-field">
                        <span class="am-input-group-btn">
            <button class="am-btn am-btn-default" type="button">搜索</button>
          </span>
                    </div>
                </div>
            </div>

            <div class="am-g">
                <div class="am-u-sm-12">
                    <form class="am-form">
                        <table class="am-table am-table-striped am-table-hover table-main">
                            <thead>
                            <tr>
                                <th class="table-check"><input type="checkbox"/></th>
                                <th class="table-id">编号</th>
                                <th class="table-title">分类名称</th>
                                <th class="table-type">缩略图</th>
                                <th class="table-type">分类商品</th>
                                <th class="table-author am-hide-sm-only">是否显示</th>
                                <th class="table-date am-hide-sm-only">排序</th>
                                <th class="table-date am-hide-sm-only">描述</th>
                                <th class="table-set">操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            @foreach($categories as $category)
                                <tr data-id="{{$category->id}}">
                                    <td><input type="checkbox"/></td>
                                    <td>{{$category->id}}</td>
                                    <td><a href="#">{{$category->name}}</a></td>
                                    <td></td>
                                    <td>
                                    </td>
                                    <td class="am-hide-sm-only">
                                        {!! is_something($category,'is_show') !!}
                                    </td>
                                    <td class="am-hide-sm-only ">
                                        <input type="text" class="sort_order" value="{{$category->sort_order}}" style="width: 40px">

                                    </td>
                                    <td class="am-hide-sm-only">{{$category->description}}</td>
                                    <td>
                                        <div class="am-btn-toolbar">
                                            <div class="am-btn-group am-btn-group-xs">
                                                <a href="{{route('shop.category.edit',$category->id)}}"
                                                   class="am-btn am-btn-default am-btn-xs am-text-secondary"><span
                                                            class="am-icon-pencil-square-o"></span> 编辑
                                                </a>

                                                <button class=" delete_one am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only">
                                                    <span class="am-icon-trash-o"></span> 删除
                                                </button>
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                                @foreach($category->children as $child)
                                    <tr data-id="{{$child->id}}" class="xChildren" >
                                        <td><input type="checkbox"/></td>
                                        <td>{{$child->id}}</td>
                                        <td>
                                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                            &nbsp;&nbsp;
                                            |--{{$child->name}}
                                        </td>
                                        <td><img src="{{$child->thumb}}" alt="" style="width: 50px;height: 50px;"></td>
                                        <td>
                                            {!! show_category_products($child) !!}
                                            {{--<a href="">查看商品</a>--}}
                                        </td>
                                        <td class="am-hide-sm-only">
                                            {!! is_something($category,'is_show') !!}
                                        </td>
                                        <td class="am-hide-sm-only ">
                                            <input type="text" class="sort_order" value="{{$child->sort_order}}" style="width: 40px">

                                        </td>
                                        <td class="am-hide-sm-only">{{$child->description}}</td>
                                        <td>
                                            <div class="am-btn-toolbar">
                                                <div class="am-btn-group am-btn-group-xs">
                                                    <a href="{{route('shop.category.edit',$child->id)}}"
                                                       class="am-btn am-btn-default am-btn-xs am-text-secondary"><span
                                                                class="am-icon-pencil-square-o"></span> 编辑
                                                    </a>

                                                    <button class=" delete_one am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only">
                                                        <span class="am-icon-trash-o"></span> 删除
                                                    </button>
                                                </div>
                                            </div>
                                        </td>
                                    </tr>
                                @endforeach
                            @endforeach

                            </tbody>
                        </table>
                        <div class="am-cf">
                            共 15 条记录
                            <div class="am-fr">
                                <ul class="am-pagination">
                                    <li class="am-disabled"><a href="#">«</a></li>
                                    <li class="am-active"><a href="#">1</a></li>
                                    <li><a href="#">2</a></li>
                                    <li><a href="#">3</a></li>
                                    <li><a href="#">4</a></li>
                                    <li><a href="#">5</a></li>
                                    <li><a href="#">»</a></li>
                                </ul>
                            </div>
                        </div>
                        <hr/>
                        <p>注：.....</p>
                    </form>
                </div>

            </div>
        </div>

        <footer class="admin-content-footer">
            <hr>
            <p class="am-padding-left">© 2014 AllMobilize, Inc. Licensed under MIT license.</p>
        </footer>

    </div>
    <!-- content end -->
@endsection
@section('js')
    <script>
        $(function () {
            //展开所有
            $('#show_all').click(function () {
                $('tr.xChildren').toggle(400);
            });
            //删除
            $('.delete_one').click(function () {
                var id = $(this).parents('tr').data('id');

                var _this = $(this);
                $.ajax({
                    type: 'DELETE',
                    url: '/admin/shop/category/' + id,
                    data: id,
                    success: function (info) {
                        if (info.status == 1) {
                            alert(info.msg);
                            _this.parents('tr').fadeOut(400);
                        } else {
                            alert(info.msg);
                        }
                    }
                });
                return false;
            })
            //改变属性
            $('.change_attr').click(function () {
                var id = $(this).parents('tr').data('id');
                var attr = $(this).attr('data-attr');
                var _this = $(this);
                $.ajax({
                    type: 'PATCH',
                    url: '{{route('shop.category.change_attr')}}',
                    data: {id: id, attr: attr},
                    success: function () {
                        _this.toggleClass('am-icon-check am-icon-close')
                    }
                })
                return false;
            })
            //排序
            $('.sort_order').change(function () {
                var info = {
                    id:$(this).parents('tr').data('id'),
                    sort_order:$(this).val()
                }
                $.ajax({
                    type:'PATCH',
                    url:"{{route('shop.category.sort_order')}}",
                    data:info,
                    success:function () {
                        window.location.reload();
                    }
                })
            })

        })
    </script>
@endsection

